package com.um.jdy.baofu.http;

import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class SimpleHttpClient {

	private HttpClient httpclient;
	private int connectionTimeout; //请求超时时间
	private int soTimeout;//读取超时
	private static final int DEFAULT_CONNECTION_TIMEOUT = 15000;//默认请求超时（单位：毫秒）
	private static final int DEFAULT_SO_TIMEOUT = 15000;//默认读取超时（单位：毫秒）


	/**
	 * 默认初始化
	 */
	public SimpleHttpClient()
	{
		this.connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
		this.soTimeout = DEFAULT_SO_TIMEOUT;
		this.httpclient = new DefaultHttpClient();
		this.httpclient.getParams().setParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout);
		this.httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, soTimeout);
	}

	/**
	 * 自定义超时初始化
	 * @param connectionTimeout
	 * @param soTimeout
	 */
	public SimpleHttpClient(int connectionTimeout, int soTimeout)
	{
		this.connectionTimeout = connectionTimeout;
		this.soTimeout = soTimeout;
		this.httpclient = new DefaultHttpClient();
		this.httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout);
		this.httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, soTimeout);
	}

	public void enableSSL() {
		try{
			SSLContext ctx = SSLContext.getInstance("TLSv1.2");
	        X509TrustManager tm = new X509TrustManager() {
	                @Override
	                public void checkClientTrusted(X509Certificate[] chain,
	                        String authType) throws CertificateException {
	                }
	                @Override
	                public void checkServerTrusted(X509Certificate[] chain,
	                        String authType) throws CertificateException {
	                }
	                @Override
	                public X509Certificate[] getAcceptedIssuers() {
	                    return null;
	                }
	        };
	        ctx.init(null, new TrustManager[]{tm}, null);
	        SSLSocketFactory ssf = new    SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
	        ClientConnectionManager ccm = httpclient.getConnectionManager();
	        SchemeRegistry sr = ccm.getSchemeRegistry();
	        sr.register(new Scheme("https", 443, ssf));
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}

	public int getSoTimeout() {
		return soTimeout;
	}
	public HttpClient getHttpclient() {
		return httpclient;
	}
	public int getConnectionTimeout() {
		return connectionTimeout;
	}
	public void setConnectionTimeout(int connectionTimeout)
	{
		this.connectionTimeout = connectionTimeout;
		this.httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout);
	}

	public void setSoTimeout(int soTimeout)
	{
		this.soTimeout = soTimeout;
		this.httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, soTimeout);
	}

}
